<template>
  <div>
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :xxl="6" :lg="8" :sm="8">
              <a-form-item label="角色">
                <a-input placeholder="请输入角色名称" v-model="queryParam.f_name" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="searchHa_role" icon="search">查询</a-button>
                <a-button icon="reload" style="margin-left: 8px" @click="reQuery">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div style="margin-bottom: 16px">
        <a-button type="primary" style="margin-left: 8px" @click="handleAdd" icon="plus">添加角色</a-button>
        <a-button type="primary" style="margin-left: 8px" @click="exportExcel" icon="export">导出角色</a-button>
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm
                :title="'已选择' + selectedRowKeys.length + '项,确定删除吗？'"
                ok-text="确定"
                cancel-text="取消"
                @confirm="delHa_roles"
              >
                <a-icon type="delete" />&nbsp;删除
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
          <a-button v-show="selectedRowKeys.length > 0" style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>
      <!-- <div class="spin-content">
        已选择&nbsp;
        <a style="font-weight: 600" v-text="selectedRowKeys.length"></a>
        项&nbsp;
        <a style="margin-left: 24px" @click="() => (selectedRowKeys = [])">清空</a>
      </div> -->
      <!--
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" -->
      <a-table
        :scroll="{ x: 1000, y: 520 }"
        :columns="ucolumns"
        :data-source="udata"
        :pagination="upagination"
        :loading="loading"
        rowKey="id"
        @change="tableChange"
        size="middle"
        :bordered="ubordered"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ (upagination.current - 1) * upagination.pageSize + parseInt(index) + 1 }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record, false)">编辑</a>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="$router.push({ name: 'ha_menuList', params: { id: record.id, name: record.name } })"
                    >菜单</a
                  >
                </a-menu-item>
                <a-menu-item>
                  <a @click="goUserList(record)">用户</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm
                    title="确定删除吗？"
                    ok-text="确定"
                    cancel-text="取消"
                    @confirm="delHa_roleById(record)"
                  >
                    <a-icon slot="icon" type="delete" />
                    <a href="javascript:;">删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </template>
        </span>
      </a-table>
    </a-card>
    <a-drawer
      :title="isView ? '角色详情' : '角色编辑'"
      :width="720"
      :visible="visible"
      :body-style="{ paddingBottom: '80px' }"
      @close="onClose"
      destroyOnClose
    >
      <edit ref="view" @close="onClose" @reloadList="searchHa_role"></edit>
    </a-drawer>
  </div>
</template>

<style lang="less" scope src="@/style/base.less"></style>

<script>
import { delHa_roles, delHa_role, queryHa_roleList, output } from '@/api/base/ha_role'
import edit from '@/views/base/Ha_roleEdit'
import { number } from 'echarts/lib/export'

export default {
  data () {
    return {
      advanced: false, // 查询条件是否展开
      loading: false, // 遮罩是否显示
      visible: false, // 抽屉是否可见
      ubordered: false, // 表格边框是否显示
      isView: false, // 是否查看
      queryParam: {
        pageNumber: undefined,
        pageSize: 20,
        f_name: null,
        sort: 'name',
        order: 'desc'
      }, // 查询条件
      selectedRowKeys: [], // 选中的行id
      udata: [], // 表格数据
      upagination: {
        // 表格分页参数
        defaultPageSize: 20,
        total: 0,
        current: 1,
        pageSize: 20,
        showTotal: total => `共${total}条数据`,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100']
      },
      ucolumns: [
        {
          title: '名称',
          dataIndex: 'name',
          width: '30%',
          sorter: (a, b) => a.name.localeCompare(b.name),
          align: 'left'
        },
        {
          title: '描述',
          dataIndex: 'description',
          width: '1',
          sorter: (a, b) => a.description.localeCompare(b.description),
          align: 'left'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '120px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ] // 表头
    }
  },
  created () {
    this.$route.query.pageNumber && (this.queryParam.pageNumber = Number(this.$route.query.pageNumber))
    this.$route.query.f_name && (this.queryParam.f_name = this.$route.query.f_name)
    this.findHa_roleList(this.queryParam)
  },
  methods: {
    /**
     *  查询角色列表
     */
    findHa_roleList (queryParam) {
      const a = Date.now()
      queryHa_roleList(queryParam).then(res => {
        this.loading = true
        this.udata = res.data.records
        // 将当前页码和数量与后台绑定
        this.upagination.total = res.data.total
        this.upagination.current = res.data.current
        this.upagination.pageSize = res.data.size
        // 如果查询时间太短就给一个0.5秒的加载效果
        const b = Date.now()
        if (b - a < 500) {
          setTimeout(() => {
            this.loading = false
          }, 500)
        } else {
          this.loading = false
        }
      })
    },
    /**
     * 重置查询条件并查询数据
     */
    reQuery () {
      this.queryParam = {}
      this.findHa_roleList(this.queryParam)
    },
    /**
     * 翻页、设置页数量时触发，将分页组建的当前页与大小传给后台
     */
    tableChange (pagination, filters, sorter, { currentDataSource }) {
      this.queryParam.pageNumber = pagination.current
      this.queryParam.pageSize = pagination.pageSize
      if (sorter != null && sorter.order != null) {
        this.queryParam.sort = sorter.field
        this.queryParam.order = sorter.order
      }
      this.findHa_roleList(this.queryParam)
    },
    /**
     * 查询
     */
    searchHa_role () {
      // 查询时将当前页码重置为1，否则会出现有数据但不显示的问题
      this.queryParam.pageNumber = 1
      // this.upagination.current = 1
      this.findHa_roleList(this.queryParam)
    },
    /**
     * 选中行数据时触发
     */
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    /**
     * 打开抽屉
     */
    showDrawer () {
      this.visible = true
    },
    /**
     * 收回抽屉
     */
    onClose () {
      this.visible = false
    },
    /**
     * 进入新增页面
     */
    handleAdd () {
      this.showDrawer()
      setTimeout(() => {
        this.$refs.view.resetHa_role()
      }, 10)
    },
    /**
     * 进入编辑页面
     */
    handleEdit (res, isView) {
      this.isView = isView
      this.showDrawer()
      setTimeout(() => {
        this.$refs.view.findHa_roleById(res.id, isView)
      }, 10)
    },
    // 进入用户列表界面
    goUserList (record) {
      this.$router.push({
        name: 'ha_role_userList',
        params: { pageNumber: this.upagination.current, f_name: this.queryParam.f_name },
        query: { id: record.id }
      })
    },
    /**
     * 删除角色
     */
    delHa_roleById (res) {
      delHa_role(res.id).then(res => {
        if (res.code === 0) {
          this.selectedRowKeys = []
          this.searchHa_role()
          this.$message.success('删除成功')
        }
      })
    },
    /**
     * 批量删除角色
     */
    delHa_roles () {
      delHa_roles({ ids: this.selectedRowKeys.toString() }).then(res => {
        if (res.code !== 0) {
          this.$message.success(res.msg)
        } else {
          this.selectedRowKeys = []
          this.searchHa_role()
          this.$message.success('删除成功')
        }
      })
    },
    /**
     *  导出Excel
     */
    exportExcel () {
      output(this.queryParam)
        .then(response => {
          var blob = new Blob([response])
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = '角色.xls' //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        })
        .catch(function () {
          console.log(error)
        })
    }
  },
  components: {
    edit
  }
}
</script>
